package kg.aibek.projecteuler.p10;

import java.util.ArrayList;
import java.util.List;

public class Problem23 {
	public void solve() {
		List<Integer> list = new ArrayList<Integer>();
		for (int i = 12; i <= 28123; i++) {
			if (isAbundant(i))
				list.add(i);
		}

		long sum = 0;
		for (int i = 1; i <= 28123; i++) {
			if (i % 1000 == 0)
				System.out.println(i);
			if (!isSumOfTwoAbundantNumber(i, list)) {
				sum += i;
			}
		}

		System.out.println(sum);

	}

	private boolean isSumOfTwoAbundantNumber(int number,
			List<Integer> abundantNumbers) {
		for (Integer n : abundantNumbers) {
			if (n > number)
				break;

			if (abundantNumbers.contains(number - n)) {
				return true;
			}
		}
		return false;
	}

	private boolean isAbundant(int number) {
		int sum = getDivisorsSum(number);
		return number < sum;
	}

	private int getDivisorsSum(int number) {
		List<Integer> list = getDivisors(number);

		int sum = 0;
		for (Integer i : list) {
			sum += i;
		}
		return sum;
	}

	private List<Integer> getDivisors(int number) {
		List<Integer> list = new ArrayList<Integer>();
		list.add(1);

		for (int i = 2; i * i <= number; i++) {
			if (number % i == 0) {
				list.add(i);
				if (i != number / i) {
					list.add(number / i);
				}
			}
		}

		return list;
	}

	public static void main(String[] args) {
		Problem23 p = new Problem23();
		long time = System.nanoTime();
		p.solve();
		long elapsed = System.nanoTime() - time;
		double seconds = (double) elapsed / 1000000000.0;
		System.out.println("Elapsed time " + seconds + " seconds");
	}
}
